package cn.demo.service0.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;

@RestController
@RefreshScope
public class Service0Controller {
    ////////////////////////////////////////////////////////////////////////////////
    @Autowired
    private Environment env;
    @GetMapping("config/{configKey}")
        //@ResponseBody
    String testConfig(@PathVariable String configKey) {
        System.out.println(configKey+":"+env.getProperty(configKey));
        return env.getProperty(configKey);
    }
    ////////////////////////////////////////////////////////////////////////////////
    @Autowired
    void setEnviroment(Environment env) {
        System.err.println("Controller0.env.from: "  + env.getProperty("from"));
    }
    ////////////////////////////////////////////////////////////////////////////////
    @Value("${from}")
    private String from;
    @RequestMapping("/hello")
    String hello() {
        return this.from;
    }
    ////////////////////////////////////////////////////////////////////////////////
    // 用于测试ribbon 重试机制
    int count = 0;
    @GetMapping("user/{userId}/{sleepSec}")
    String user(@PathVariable String userId,  @PathVariable int sleepSec) {
        try {
            System.out.println("Appliction0.user:" + userId);
            count++;
            TimeUnit.SECONDS.sleep(sleepSec - count);
            return "Appliction0.user:" + userId;
        } catch (Throwable e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }
    @PostMapping("test")
    String post(@RequestBody User user) {
        System.out.println("Appliction0.test:"+user.toString());
        return "Appliction0.test:"+user.toString();
    }
    ////////////////////////////////////////////////////////////////////////////////
    public static class User {
        private int id;
        private String name;
        private Student student;
        public User() {
        }
        public int getId() {
            return id;
        }
        public User setId(int id) {
            this.id = id;
            return this;
        }
        public String getName() {
            return name;
        }
        public User setName(String name) {
            this.name = name;
            return this;
        }
        public Student getStudent() {
            return student;
        }
        public User setStudent(Student student) {
            this.student = student;
            return this;
        }
        @Override
        public String toString() {
            return "User{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", student=" + student +
                    '}';
        }
    }
    public static class Student {
        private String stuName;
        public Student() {
        }
        public String getStuName() {
            return stuName;
        }
        public Student setStuName(String stuName) {
            this.stuName = stuName;
            return this;
        }
        @Override
        public String toString() {
            return "Student{" +
                    "stuName='" + stuName + '\'' +
                    '}';
        }
    }
}